import Express from "express";
import MarkerPoiService from "../../service/MarkerPoiService";
import ResponseHelper from "../utils/ResponseHelper";
import { ITokenRequest } from "../tokenMiddleware";

const router = Express.Router();

/**
 * 查找用户自己的所有标记点
 */
router.get("/", async (req: ITokenRequest, res, next) => {
  const { UID } = req;
  try {
    const result = await MarkerPoiService.findByUid(UID);

    ResponseHelper.send(result, res, req);
  } catch (err) {
    next(err);
  }
});

/**
 * 根据 用户 id 查找标记点
 */
router.get("/findById", async (req: ITokenRequest, res, next) => {
  const { uid } = req.query;
  try {
    const result = await MarkerPoiService.findByUid(+uid);

    ResponseHelper.send(result, res, req);
  } catch (err) {
    next(err);
  }
});

// TODO 这个 API 是否可以去除
router.get("/attrname", async (req: ITokenRequest, res, next) => {
  const m_id = req.query.m_id as string;
  try {
    const result = await MarkerPoiService.getNameByMId(m_id);

    ResponseHelper.send(result, res, req);
  } catch (err) {
    next(err);
  }
});

router.post("/", async (req: ITokenRequest, res, next) => {
  const { UID } = req;

  try {
    req.body.u_id = UID;
    const result = await MarkerPoiService.add(req.body);
    ResponseHelper.send(result, res, req);
  } catch (err) {
    next(err);
  }
});

router.delete("/:id", async (req: ITokenRequest, res, next) => {
  const { UID } = req;

  try {
    const result = await MarkerPoiService.del(UID, req.params.id);

    return ResponseHelper.send(result, res, req);
  } catch (err) {
    next(err);
  }
});

router.put("/", async (req: ITokenRequest, res, next) => {
  const { UID } = req;

  try {
    req.body.u_id = UID;
    const result = await MarkerPoiService.modify(req.body);

    ResponseHelper.send(result, res, req);
  } catch (err) {
    next(err);
  }
});

export default router;
